package com.brillilab.service.logic.crm;


import com.brillilab.common.constant.ClientTypeEnum;
import com.brillilab.common.constant.KeyProfixConstant;
import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.constant.TimeConstant;
import com.brillilab.common.exceptions.BrillilabException;
import com.brillilab.common.redis.RedisCache;
import com.brillilab.common.utils.CodeUtil;
import com.brillilab.common.utils.MyBeanUtils;
import com.brillilab.domain.enums.crm.CrmUserEnum;
import com.brillilab.domain.po.crm.CrmUsers;
import com.brillilab.domain.vo.crm.CrmUsersInfoVo;
import com.brillilab.service.core.crm.ICrmUsersService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;


@Service
@Transactional
public class CrmUsersLogic {

    @Resource
    private ICrmUsersService crmUsersService;

    @Resource
    private RedisCache redisCache;

    public CrmUsersInfoVo userLogin(String userName, String password) {
        CrmUsers crmUsers = crmUsersService.selectUser(userName, null);
        Assert.notNull(crmUsers, "该用户名未注册!");
        crmUsers = crmUsersService.selectUser(userName, password, null);
        // 用户不存在
        Assert.notNull(crmUsers, "密码错误!");
        Assert.isTrue(crmUsers.getUserState() == CrmUserEnum.State.NORMAL.getValue(),
                "该用户已被" + CrmUserEnum.State.getKey(crmUsers.getUserState()) + "!");
        CrmUsersInfoVo crmUsersInfoVo = MyBeanUtils.copyBean(crmUsers, CrmUsersInfoVo.class);
        // 设置用户登录信息缓存
        this.setUserCache(crmUsersInfoVo, ClientTypeEnum.CRM);
        return crmUsersInfoVo;
    }


    /**
     * 设置用户登录信息缓存
     *
     * @param crmUsersInfoVo
     * @param clientType
     */
    public void setUserCache(CrmUsersInfoVo crmUsersInfoVo, ClientTypeEnum clientType) {
        if (crmUsersInfoVo != null) {
            // 设置用户登录信息缓存
            String token = CodeUtil.getToken(clientType);
            crmUsersInfoVo.setToken(token);
            redisCache.set(KeyProfixConstant.TOKEN + token, crmUsersInfoVo, TimeConstant.HARF_HOUR);
        }
    }

    /**
     * Token获取用户信息
     *
     * @return CrmUsersInfoVo
     */
    public CrmUsersInfoVo getCrmUserByToken(String token) {
        // 通过Token获取用户信息
        CrmUsersInfoVo crmUsersInfoVo = (CrmUsersInfoVo)redisCache.getObj(KeyProfixConstant.TOKEN +token);
        if (crmUsersInfoVo != null) {
            // 重置缓存时效
            redisCache.set(KeyProfixConstant.TOKEN +token, crmUsersInfoVo, TimeConstant.HARF_HOUR);
        }
        if(crmUsersInfoVo==null){
            throw new BrillilabException(ResultEnum.USER_NOT_LOGIN,"由于您长时间未操作，请重新登陆！");
        }
        return crmUsersInfoVo;
    }

    /**
     * 校验是否为crm用户
     * @param token
     * @return
     */
    public boolean checkTokenIsCrmUser(String token){
        String client = token.split("_")[0];
        return client.equals(ClientTypeEnum.CRM.getValue());
    }
}
